package com.chuantou.yuba.util;

import java.util.List;

import android.app.Activity;
import android.text.TextUtils;

import com.chuantou.yuba.YBHXSDKHelper;
import com.chuantou.yuba.api.to.UserTO;
import com.easemob.EMCallBack;
import com.easemob.applib.controller.HXSDKHelper;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMMessage;
import com.easemob.chat.EMMessage.Direct;
import com.easemob.exceptions.EaseMobException;

public class ChatUtils {
	private static final String HX_UID_PREFIX = "diaoyuba";

	public static final String getHXAccount(long uid) {
		return HX_UID_PREFIX.concat(String.valueOf(uid));
	}

	public static final long getUid(String hxAccount) {
		if (TextUtils.isEmpty(hxAccount))
			return 0;
		String result = hxAccount.replace(HX_UID_PREFIX, "");
		if (TextUtils.isDigitsOnly(result)) {
			return Long.parseLong(result);
		} else
			return 0;
	}

	public static final String getChatNickname(EMMessage message) throws EaseMobException {
		String attrTargetNickname = message.getStringAttribute(Constants.ATTR_TARGET_NICKNAME);
		String attrNickname = message.getStringAttribute(Constants.ATTR_NICKNAME);
		return message.direct == Direct.SEND ? attrTargetNickname : attrNickname;
	}

	public static final String getChatAvatar(EMMessage message) throws EaseMobException {
		String attrAvatar = message.getStringAttribute(Constants.ATTR_AVATAR);
		String attrTargetAvatar = message.getStringAttribute(Constants.ATTR_TARGET_AVATAR);
		return message.direct == Direct.SEND ? attrTargetAvatar : attrAvatar;
	}

	public static final String getFollower(EMMessage message) throws EaseMobException {
		return message.getStringAttribute(Constants.ATTR_FOLLOWER);
	}

	public static final String getFollowerAvatar(EMMessage message) throws EaseMobException {
		return message.getStringAttribute(Constants.ATTR_FOLLOWER_AVATAR);
	}

	public static final void loginIM(final Activity activity) {
		AccountManager accountManager = AccountManager.getInstance(activity.getApplicationContext());
		if (accountManager.isLogined()) {
			UserTO user = accountManager.getUserTO();
			// 登录聊天服务器
			HXSDKHelper helper = YBHXSDKHelper.getInstance();
			if (!helper.isLogined()) {
				EMChatManager.getInstance().login(user.getHxAccount(), user.getHxpwd(), new EMCallBack() {

					@Override
					public void onSuccess() {
						EMChatManager.getInstance().loadAllConversations();
						sysBlackList();
					}

					@Override
					public void onProgress(int arg0, String arg1) {

					}

					@Override
					public void onError(int arg0, String arg1) {

					}
				});
			}
		}
	}

	public static void sysBlackList() {
		try {
			// 获取黑名单列表
			List<String> blackList = EMContactManager.getInstance().getBlackListUsernamesFromServer();
			// 保存黑名单
			EMContactManager.getInstance().saveBlackList(blackList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
